package com.zengqingfa.kafka.producer;

import org.apache.kafka.clients.producer.*;

import java.util.Properties;

/**
 * @author zengqingfa
 * @className MyProducer3
 * @description 同步发送
 * @create 2021/8/1 15:36
 * KafkaProducer：需要创建一个生产者对象，用来发送数据
 * ProducerConfig：获取所需的一系列配置参数
 * ProducerRecord：每条数据都要封装成一个 ProducerRecord 对象
 */
public class MyProducer3 {

    public static void main(String[] args) throws Exception {
        Properties props = new Properties();
        //kafka 集群，broker-list
        props.put("bootstrap.servers", "localhost:9092");
        props.put("acks", "all");
        //重试次数
        props.put("retries", 1);
        //批次大小
        props.put("batch.size", 16384);
        //等待时间
        props.put("linger.ms", 1);
        //RecordAccumulator 缓冲区大小
        props.put("buffer.memory", 33554432);
        props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        Producer<String, String> producer = new KafkaProducer<>(props);
        for (int i = 0; i < 20; i++) {
            RecordMetadata recordMetadata = producer.send(new ProducerRecord<String, String>("test.topic",
                    Integer.toString(i), Integer.toString(i))).get();
            System.out.println(recordMetadata.toString());
        }
        Thread.sleep(10000);
        producer.close();
    }
}